define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Form = require("form");
	var Dialog = require("dialog");
	var store =require("store");
	var activeUser = store.get("user");	
    var instance = {
        initComponent: function (widget) {
        	var form = new Form({
        		show:false,
        		parentNode:".J-form",
        		saveaction:function(){
        			var data = form.getData();
        			aw.ajax({
        				url:"api/destnbuilding/query",
        				data:{
        					"orgBuilding":form.getValue("orgBuilding"),
        					fetchProperties:"pkDestnBuilding,name"
        				},
        				dataType:"json",
        				success:function(datas){
        					if(datas.length>0 && datas[0].pkDestnBuilding != data.pkDestnBuilding){
        						Dialog.alert({
        							content:"该名称已存在,请输入其他名称！",
        							confirm : function(){
    	     							form.setValue("orgBuilding","");
    	     							Dialog.close();
    	      						}
        						});
        						return;
        					}else{
        						aw.saveOrUpdate("api/destnbuilding/save",data,function(data){
        	        				widget.get("grid").refresh();
        							widget.show([".J-grid"]).hide([".J-form"]);
        							widget.get("subnav").hide(["return"]).show(["add","search","servicePoint"]);
        	        			});
        					}
        				}
        			});
        		},
        		cancelaction:function(){
					widget.show([".J-grid"]).hide([".J-form"]);
					widget.get("subnav").hide(["return"]).show(["search","add","servicePoint"]);
					widget.get("grid").refresh();
				},
				model:{
					id:"destnbuilding",
					layout:"2_1",
					items:[{
						name:"servicePoint",
						label:"服务点名称",
						type:"select",
						validate:["required"],
						items:activeUser.privateServicePoints,
						url:"api/servicepoint/operableservicepoints",
						keyField : "pkServicePoint",
						valueField : "name",
						events:{
							"change":function(e){
								var form=widget.get("form");
								var pkServicePoint=form.getValue("servicePoint");
								var obj={
										"servicePoint":pkServicePoint,
										fetchProperties:"pkOrgBuilding,name,floors,roomCount"
								};
								aw.ajax({
									url:"api/destnbuilding/querybuilding",
									data:obj,
									dataType:"json",
									success:function(data){
										form.setData("orgBuilding", data);
										widget.set("temp",data);
									}
								});
							}
						}
					},{
						name:"orgBuilding",
						label:"楼宇名称",
						type : "select",
						validate:["required"],
						url:"api/destnbuilding/querybuilding",
						keyField : "pkOrgBuilding",
						valueField : "name",
						lazy: true,
						events: {
							"change": function(e){
								var form = widget.get("form");
								var list =widget.get("temp")
				        		var pkOrgBuilding = form.getValue("orgBuilding");
								var orgBuilding={};
								for(var i in list){
									if(list[i].pkOrgBuilding==pkOrgBuilding){
										orgBuilding=list[i];
									}
								}
								if(orgBuilding!=null){
									form.setValue("floors",orgBuilding.floors);
					        		form.setValue("roomCount",orgBuilding.roomCount);
								}
							}
						}
					},{
						name:"floors",
						label:"总层数",
						readonly: true
					},{
						name:"destination",
						label:"所属目的地",
						type : "select",
						validate:["required"],
						url:"api/destination/query",
						keyField : "pkDestination",
						valueField : "name",
						params:function(){
							return { 
							status:"Setting"
							}
						}
					},{
						name:"roomCount",
						label:"房间总数",
						readonly: true
					},{
						name:"version",
						type:"hidden",
						defaultValue:"0"
					},{
						name:"pkDestnBuilding",
						type:"hidden"
					}]
				
				}
        	});
        	widget.set("form", form);
        }
    };
    module.exports = instance;
});